package com.lcz.googleplay.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.lcz.googleplay.holder.BaseHolder;
import com.lcz.googleplay.holder.MoreHolder;
import com.lcz.googleplay.manager.ThreadManager;
import com.lcz.googleplay.utils.ToastUtils;
import com.lcz.googleplay.utils.UIUtils;

import java.util.List;

/**
 * ListView适配器基类
 * Created by hp on 2016/10/28.
 */

public abstract class DefaultAdapter<T> extends BaseAdapter implements AdapterView.OnItemClickListener {
    private List<T> list;
    private static final int DEFAULT_ITEM = 0;//默认条目
    private static final int MORE_ITEM = 1;//加载更多条目
    private ListView listView;

    public DefaultAdapter(List<T> list, ListView listView) {
        this.list = list;
        this.listView = listView;
        listView.setOnItemClickListener(this);
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size() + 1;//最后一个条目就是加载更多
    }

    /*当前条目类型*/
    @Override
    public int getItemViewType(int position) {
        if (position == list.size()) {//最后一个条目，加载更多
            return MORE_ITEM;
        } else {
            return DEFAULT_ITEM;
        }
        //return super.getItemViewType(position);
    }

    /*当前ListView的条目类型*/
    @Override
    public int getViewTypeCount() {
        return super.getViewTypeCount() + 1;//一种数据显示类型，一种加载更多显示类型
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        BaseHolder holder = null;
        //先判断当前条目类型
        switch (getItemViewType(i)) {
            case MORE_ITEM://显示当前数据的最后一条，需要加载更多
                if (convertView == null) {
                    holder = getMoreHolder();
                } else {
                    holder = (BaseHolder<T>) convertView.getTag();
                }
                break;
            case DEFAULT_ITEM://正常加载当前数据
                if (convertView == null) {
                    holder = getHolder();
                } else {
                    holder = (BaseHolder<T>) convertView.getTag();
                }
                if (i < list.size()) {
                    holder.setData(list.get(i));
                }
                break;
        }

        return holder.getView();
    }

    private MoreHolder moreHolder;

    private BaseHolder getMoreHolder() {
        if (moreHolder != null) {
            return moreHolder;
        } else {
            moreHolder = new MoreHolder(this);
            return moreHolder;
        }

    }

    public abstract BaseHolder<T> getHolder();

    public void loadMore() {
        //连接服务器加载数据
        ThreadManager.getInstance().createLongPool().execute(new Runnable() {
            @Override
            public void run() {
                final List<T> newList = onload();
                //子线程加载数据，之后显示在界面，所以返回主线程执行
                UIUtils.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (newList == null) {//连接服务器失败
                            moreHolder.setData(MoreHolder.LOAD_ERROR);
                        } else if (newList.size() == 0) {//没有更多数据
                            moreHolder.setData(MoreHolder.HAS_NO_MORE);
                        } else {
                            moreHolder.setData(MoreHolder.HAS_MORE);
                            //成功加载数据
                            list.addAll(newList);
                            notifyDataSetChanged();
                        }
                    }
                });
            }
        });
    }

    protected abstract List<T> onload();

    /*ListView条目点击事件*/
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//        ToastUtils.show("position==" + position);
        //HomeFragment的ListView有广告条目，所以需要处理条目位置，让子类处理点击事件
        position = position - listView.getHeaderViewsCount();
        onInnerItemClick(position);
    }

    public void onInnerItemClick(int position) {

    }
}
